﻿using BrightIdeasSoftware;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static FormTest.UcVirtualListView;

namespace FormTest
{
    public partial class Form7 : Form
    {
        private VirtualObjectListView _olv;
        private List<User> _users; // 正确的数据源类型（IList<T>）
        public Form7()
        {
            // InitializeComponent();
            InitializeComponent1();
            InitializeVOLV();
            LoadAndBindData();
            SetupEvents();
        }

        // 设计器代码
        private void InitializeComponent1()
        {
            SuspendLayout();
            ClientSize = new Size(800, 500);
            Name = "MainForm";
            Text = "VirtualObjectListView 正确绑定示例";
            ResumeLayout(false);
        }
        // 初始化VirtualObjectListView
        private void InitializeVOLV()
        {
            _olv = new VirtualObjectListView
            {
                Dock = DockStyle.Fill,
                View = View.Details,
                GridLines = true,
                FullRowSelect = true,
                //CacheVirtualItems+=XXXX,
                RowHeight = 24
            };

            // 配置列（AspectName与User属性严格匹配）
            _olv.Columns.AddRange(new OLVColumn[]
            {
                new OLVColumn("ID", "Id") { Width = 60 },
                new OLVColumn("姓名", "Name") { Width = 120 },
                new OLVColumn("年龄", "Age") { Width = 80 },
                new OLVColumn("部门", "Department") { Width = 150 }
            });

            Controls.Add(_olv);
        }

        // 加载并绑定数据
        private void LoadAndBindData()
        {
            // 生成测试数据（10万条，体现虚拟模式优势）
            _users = Enumerable.Range(1, 10)
                              .Select(i => new User
                              {
                                  Id = i,
                                  Name = $"用户{i}",
                                  Age = 18 + (i % 30),
                                  Department = i % 3 == 0 ? "技术部" :
                                               i % 3 == 1 ? "市场部" : "人事部"
                              })
                              .ToList(); // 转换为List<T>（关键）

            // 绑定数据（必须用SetObjects）
            _olv.SetObjects(_users);
        }

        // 事件配置（添加/更新数据）
        private void SetupEvents()
        {
            // 添加数据按钮
            var addBtn = new Button { Text = "添加用户", Dock = DockStyle.Top, Height = 30 };
            addBtn.Click += (s, e) => {
                var newUser = new User
                {
                    Id = _users.Max(u => u.Id) + 1,
                    Name = $"新用户{DateTime.Now.Second}",
                    Age = 25,
                    Department = "新部门"
                };
                _users.Add(newUser);
                _olv.SetObjects(_users); // 刷新
                //_olv.EnsureVisible(_users.Count - 1); // 滚动到新项
            };

            // 异步添加按钮
            var asyncAddBtn = new Button { Text = "异步添加", Dock = DockStyle.Top, Height = 30 };
            asyncAddBtn.Click += async (s, e) => {
                await Task.Run(() => {
                    // 后台生成数据
                    var newUser = new User
                    {
                        Id = _users.Max(u => u.Id) + 1,
                        Name = $"异步用户{DateTime.Now.Millisecond}",
                        Age = 30,
                        Department = "异步部门"
                    };
                    _users.Add(newUser);
                });

                // UI线程刷新
                _olv.Invoke((Action)(() => {
                    _olv.SetObjects(_users);
                    _olv.EnsureVisible(_users.Count - 1);
                }));
            };

            Controls.Add(addBtn);
            Controls.Add(asyncAddBtn);
        }
    }
    // 数据模型（必须有公共属性）
    public class User
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public string Department { get; set; }
    }
}
